#include "newdownloaddialog.h"

#include <QDebug>

NewDownloadDialog::NewDownloadDialog(QWidget *parent) :
    QDialog(parent)
{
    setWindowTitle("New download");

    QGridLayout *gridLayout = new QGridLayout(this);

    gridLayout->addWidget(new QLabel("URL:"), 0, 0);
    gridLayout->addWidget(new QLabel("Name:"), 1, 0);
    gridLayout->addWidget(new QLabel("Save to:"), 2, 0);
    gridLayout->addWidget(new QLabel("Priority:"), 3, 0);

    internetAddressEdit = new QLineEdit();
    gridLayout->addWidget(internetAddressEdit, 0, 1, 1, 2);

    nameEdit = new QLineEdit();
    gridLayout->addWidget(nameEdit, 1, 1, 1, 2);

    localAddressEdit = new QLineEdit("/");
    gridLayout->addWidget(localAddressEdit, 2, 1);

    saveButton = new QPushButton("Save");
    gridLayout->addWidget(saveButton, 2, 2);
    connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));

    priorityComboBox = new QComboBox(this);
    QStringList priorities;
    priorities << "0" << "1" << "2" << "3" << "4" << "5";
    priorityComboBox->addItems(priorities);
    gridLayout->addWidget(priorityComboBox, 3, 1, 1, 2);

    downloadButton = new QPushButton("Download");
    downloadButton->setFocus();
    connect(downloadButton,SIGNAL(clicked()), this, SLOT(accept()));
    gridLayout->addWidget(downloadButton, 4, 1);

    QPushButton *cancelButton = new QPushButton("Cancel");
    connect(cancelButton,SIGNAL(clicked()), this, SLOT(reject()));
    gridLayout->addWidget(cancelButton, 4, 2);

    setLayout(gridLayout);
}

void NewDownloadDialog::saveButtonClicked(){
    QString dir = QFileDialog::getExistingDirectory(this, "Open Directory", "/",
        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    localAddressEdit->setText(dir);
}

QString NewDownloadDialog::getInternetAddress(){
    return internetAddressEdit->text();
}

QString NewDownloadDialog::getLocalAddress() {
    if (localAddressEdit->text() == "/") {
        return localAddressEdit->text() + nameEdit->text();
    } else {
        return localAddressEdit->text() + '/' + nameEdit->text();
    }
}

quint8 NewDownloadDialog::getPriority() {
    return priorityComboBox->currentIndex();
}

